#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>

using namespace std;

const int N = 1e5 + 10;

int q[N], hh, tt;

int main()
{
    int n;
    cin >> n;

    while (n--)
    {
        string c;
        cin >> c;
        int x;

        if (c == "push")
        {
            cin >> x;

            q[tt++] = x;
        }
        else if (c == "pop") tt--;
        else if (c == "query") printf("%d\n", q[hh]);
        else cout << (hh == tt ? "YES" : "NO") << endl;
    }

    return 0;

}